//
//  RoadShowViewController.m
//  BigToBig
//
//  Created by liuyi on 7/21/16.
//  Copyright © 2016 cn.cloudtp. All rights reserved.
//

#import "RoadShowViewController.h"
#import "PullDownButton.h"
#import "MyRoadShowController.h"
#import "JoinedRoadShowController.h"
#import "QuickMatingRoadShowController.h"

@interface RoadShowViewController ()<UITextFieldDelegate,UIScrollViewDelegate>
{
    
    
    UIView *pageV;
    UIView *titleV;
    UIView *functionV;
    UIImageView *cornerV;
    PullDownButton *selectedBtn;
    PullDownButton *selectedBtn1;
    UIScrollView *scrollV;
    
    PullDownButton *projectBtn;
    PullDownButton *entrepreneurBtn;
    PullDownButton *investorBtn;
    UIButton *sortBtn;
    UIButton *filterBtn;
    
    BOOL isMove; //手动滑动scrollView的时候不触发点击事件的滑动效果
    
}
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic ,weak)UITextField *titleText;
//@property(nonatomic,strong)UnUseController *unUseVc;
//@property(nonatomic,strong)DidUseController *didUseVc;
//@property(nonatomic,strong)ExpiredController *expiredVc;
@property (nonatomic, strong) MyRoadShowController *myRoadShowVC;
@property (nonatomic, strong) JoinedRoadShowController *joinedVC;
@property (nonatomic , strong) QuickMatingRoadShowController *quickMatingVC;



@end
@implementation RoadShowViewController

- (void)viewWillAppear:(BOOL)animated{

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    isMove = NO ;
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationController.navigationBar.translucent = YES;
    [self addThreeBtn];
    [self addScrollView];
    [self setBackBar];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = COLOR_RGBA(77, 155, 255, 1);
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 64)];
    titleLabel.text = @"路演&速配";
    [titleLabel setFont:[UIFont systemFontOfSize:20]];
    titleLabel.textAlignment = 1;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    [self.navigationItem setTitleView:titleLabel];
    
    scrollV.showsHorizontalScrollIndicator = NO;
    
    scrollV.bounces = NO;

}
#pragma mark 导航栏左右Button
- (void)setBackBar{
    
    BackButton *backBtn=[BackButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 44, 44);
    [backBtn addTarget:self action:@selector(backController:) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    [backBtn.imageView setContentMode:UIViewContentModeLeft];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    
}
#pragma mark 返回事件
- (void)backController:(UIButton *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (MyRoadShowController *)myRoadShowVC{
    if (_myRoadShowVC == nil) {
        _myRoadShowVC = [[MyRoadShowController alloc]init];
        
    }
    [self addChildViewController:_myRoadShowVC];
    return _myRoadShowVC;
}

- (JoinedRoadShowController *)joinedVC{
    if (_joinedVC == nil) {
        _joinedVC = [[JoinedRoadShowController alloc]init];
    }
    [self addChildViewController:_joinedVC];
    return _joinedVC;
}

- (QuickMatingRoadShowController *)quickMatingVC{
    if (_quickMatingVC == nil) {
        _quickMatingVC = [[QuickMatingRoadShowController alloc]init];
    }
    [self addChildViewController:_quickMatingVC];
    return _quickMatingVC;
}
- (void)addThreeBtn{
    
    titleV = [[UIView alloc]initWithFrame:CGRectMake(20, 84, kScreenWidth - 40, 20)];
    [self.view addSubview:titleV];
    projectBtn = [PullDownButton createButtonWithTitle:@"我的路演" frame:CGRectMake(0,0,(kScreenWidth - 40)/3 ,20) tag:1000];
    [titleV addSubview:projectBtn];
    [projectBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    entrepreneurBtn = [PullDownButton createButtonWithTitle:@"参与的路演" frame:CGRectMake(projectBtn.maxX , 0,(kScreenWidth - 40)/3 ,20) tag:1001];
    [titleV addSubview:entrepreneurBtn];
    [entrepreneurBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    investorBtn = [PullDownButton createButtonWithTitle:@"路演速配" frame:CGRectMake(entrepreneurBtn.maxX ,0,(kScreenWidth - 40) / 3 , 20) tag:1002];
    [titleV addSubview:investorBtn];
    [investorBtn addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    if (selectedBtn1==nil) {
        
        projectBtn.selected = YES;
        
        [projectBtn.titleLabel setFont:[UIFont systemFontOfSize:projectBtn.frame.size.width / 6 * 5 / 6 + 1]];
        projectBtn.downImageView.image = [UIImage imageNamed:@"xl-selected"];
        
        selectedBtn1 = projectBtn;
    }
    
}



- (void)buttonClickAction:(PullDownButton *)button{
    if (selectedBtn1 == button) {
        return;
    }
    else{
        button.selected = YES;
        selectedBtn1.selected = NO;
        [button.titleLabel setFont:[UIFont systemFontOfSize:17]];
        button.downImageView.image = [UIImage imageNamed:@"xl-selected"];
        [selectedBtn1.titleLabel setFont:[UIFont systemFontOfSize:projectBtn.frame.size.width / 6 * 5 / 6 ]];
        selectedBtn1.downImageView.image = [UIImage imageNamed:@"xl-button"];
        selectedBtn1 = button;
        [UIView animateWithDuration:.3 animations:^{
            if (!isMove) {
                
                [scrollV setContentOffset:CGPointMake((button.tag - 1000) * kScreenWidth, 0)];
            }
            
        } completion:^(BOOL finished) {
            
            isMove = NO;
        }];
        
    }
    
    
}


- (void)addScrollView{
    
    
    scrollV=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 115, kScreenWidth, kScreenHeight-115)];
    scrollV.backgroundColor = [UIColor redColor];
    scrollV.contentSize = CGSizeMake(3*kScreenWidth, scrollV.height);
    scrollV.pagingEnabled = YES;
    scrollV.delegate = self;
    [self.view addSubview:scrollV];
    self.myRoadShowVC.view.frame = CGRectMake(0, 0, kScreenWidth, scrollV.height);
    [scrollV addSubview:_myRoadShowVC.view];
    self.joinedVC.view.frame = CGRectMake(kScreenWidth , 0 , kScreenWidth, scrollV.height);
    [scrollV addSubview:_joinedVC.view];
    self.quickMatingVC.view.frame = CGRectMake(kScreenWidth * 2, 0, kScreenWidth, scrollV.height);
    [scrollV addSubview:_quickMatingVC.view];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    isMove = YES;
    
    CGFloat offsetX = scrollView.contentOffset.x;
    
    if (offsetX >= 0 &&offsetX<kScreenWidth / 2) {
        [self buttonClickAction:projectBtn];
    }
    else if(offsetX >= kScreenWidth / 2 && offsetX < kScreenWidth / 2 * 3){
        [self buttonClickAction:entrepreneurBtn];
    }
    else if(offsetX >= kScreenWidth / 2 * 3){
        [self buttonClickAction:investorBtn];
    }
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    isMove = NO;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.titleText endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return [self.titleText endEditing:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
